package xyz.zdsoft.auth_module.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import xyz.zdsoft.auth_module.dto.LoginRequest;
import xyz.zdsoft.auth_module.service.AuthService;
import xyz.zdsoft.auth_module.utils.CheckCode;
import xyz.zdsoft.common.utils.JwtUtils;
import xyz.zdsoft.common.utils.Result;
import xyz.zdsoft.common.utils.ResultBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Api(value = "登录接口", tags = "用户登录")
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    private final AuthService authService;

    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    @ApiOperation(value = "用户登录", notes = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest) {
        return authService.login(
                loginRequest.getUserNo(),
                loginRequest.getPassword(),
                loginRequest.getRoleType(),
                loginRequest.getCheckCode(),
                loginRequest.getCheckId()
        );
    }

    @ApiOperation(value = "退出登录", notes = "退出登录")
    @PostMapping("/loginOut")
    public Result loginOut(HttpServletRequest request) {
        return authService.loginOut(request);
    }

    @ApiOperation(value = "请求验证码Id", notes = "请求验证码")
    @GetMapping("/getCodeId")
    public Result getCodeId(){
        return CheckCode.getCodeId();
    }

    @ApiOperation(value = "请求验证码", notes = "请求验证码")
    @GetMapping("/getCode")
    public void getCode(HttpServletResponse response){
        CheckCode.getImage(response);
    }

    @ApiOperation(value = "测试", notes = "测试")
    @GetMapping("/getToken")
    public ResultBody getToken(HttpServletRequest request){
        Map map  = new HashMap();
        map.put("hhh","hhh123");
//        if(1==1)throw new BizException("400","错误的请求");
//        if(1==1)throw new BizException(CommonEnum.INTERNAL_SERVER_ERROR);
        String a = "asd123";
        Integer.parseInt(a);
      /*  String a = null;
        a.equals("123");*/
        return ResultBody.success(map);
    }

    @ApiOperation(value = "测试2", notes = "测试")
    @GetMapping("/getToken2")
    public String getToken2(HttpServletRequest request){

        return JwtUtils.getMemberIdByJwtToken(request);
    }


    @ApiOperation(value = "测试2", notes = "测试")
    @GetMapping("/getToken1")
    public String getToken1(HttpServletRequest request){
        return JwtUtils.getMemberIdByJwtToken(request);
    }

}
